package com.gitee.controller;

import com.gitee.bo.UserLoginBo;
import com.gitee.service.IUserService;
import com.gitee.utils.JwtUtil;
import com.gitee.vo.ResponseVo;
import com.gitee.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public ResponseVo<String> login(@RequestBody UserLoginBo userLoginBo) {
        String token = userService.login(userLoginBo);
        return ResponseVo.ok(token);
    }

    @GetMapping("/info")
    public ResponseVo<UserVo> info(HttpServletRequest request) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        UserVo userVo = userService.info(userId);
        return ResponseVo.ok(userVo);
    }

}
